package com.example.back.mapper;

import java.util.List;
import java.util.Map;

import com.example.common.entity.RxInfo;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

public interface InquiryOrderMapper {
    @SelectProvider(type = SqlBuilder.class, method = "ListSQL")
    List<Map<String, Object>> List(@Param("orderno") String orderno, @Param("atype") int atype,
            @Param("dname") String dname, @Param("cname") String cname, @Param("ocstatus") int ocstatus,
            @Param("skip") int skip, @Param("limit") int limit);

    @SelectProvider(type = SqlBuilder.class, method = "ListCountSQL")
    Integer ListCount(@Param("orderno") String orderno, @Param("atype") int atype, @Param("dname") String dname,
            @Param("cname") String cname, @Param("ocstatus") int ocstatus);

            @Select("with paget as ("
            + " select id_bigint,create_time_datetime from im_consultation_log where oc_id_bigint = #{ocid}"
            + " order by create_time_datetime desc"
            + " limit #{skip},#{limit}),"
            + " pageo as ("
            + " select id_bigint from paget order by create_time_datetime)"
            + " select cast(a.im_type_tinyint as SIGNED INTEGER) as IMType,a.im_content_nvarchar as IMContent,cast(a.c_type_tinyint as SIGNED INTEGER) as CType,a.create_time_datetime "
            + " from pageo p"
            + " inner join im_consultation_log a on a.id_bigint = p.id_bigint")
            List<Map<String, Object>> findByOCID(@Param("ocid") long ocid, @Param("skip") int skip, @Param("limit") int limit);

            @Select("select a.name_nvarchar as Name,a.sex_nvarchar as Sex,a.age_int as Age,b.a_type_int as AType,c.`name` as Name,c.title as Title,"
            + " d.name_nvarchar as depName,b.receive_time_datetime as ReceiveTime,a.chief_complaint_nvarchar as Chiefcomplaint,a.allergy_nvarchar as Allergy,"
            + " a.past_disease_nvarchar as PastDisease,a.f_visit_date_date as FVisitDate,a.f_hospital_nvarchar as FHospital,"
            + " a.f_department_nvarchar as FDepartment,a.f_diagnosis_nvarchar as FDiagnosis,a.id_bigint as ID,"
            + " a.first_tinyint as firstType,a.is_pregnancy_bit as isPregnancy,a.is_lactation_period_bit as isLactationPeriod"
            + " from order_patients_info a"
            + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " inner join doctor_base_info c on b.dc_id_bigint = c.dc_id"
            + " inner join h_hospital_department d on c.depart_id = d.id_bigint"
            + " where a.order_no_nvarchar = #{orderno}")
            Map<String, Object> DiagnosisOut(@Param("orderno") String orderno);
            
            @Select("select img_nvarchar from visit_img where patients_info_id_bigint = #{id}")
            String[] visitImgList(@Param("id") long id);

            @Select("select a.create_time_datetime as CreateTime,a.rx_order_no_nvarchar as RxOrderNO,cast(a.d_type_tinyint as SIGNED INTEGER) as DType,"
            + " c.name_nvarchar as Name,c.sex_nvarchar as Sex,c.age_int as Age,e.name_nvarchar as depName,cast(a.chk_status_tinyint as SIGNED INTEGER) as ChkStatus,"
            + " a.open_doctor_nvarchar as OpenDoctor,a.chk_doctor_nvarchar as ChkDoctor,a.failure_reason_nvarchar as FailureReason,"
            + " a.failure_remark_nvarchar as FailureRemark,a.dialectical_nvarchar as Dialectical"
            + " from order_rx_main a"
            + " inner join order_main b on a.order_no_nvarchar = b.order_no_nvarchar"
            + " inner join order_patients_info c on b.patients_info_id_bigint = c.id_bigint"
            + " inner join doctor_base_info d on b.dc_id_bigint = d.dc_id"
            + " inner join h_hospital_department e on d.depart_id = e.id_bigint"
            + " where a.order_no_nvarchar = #{orderno}"
            + " order by a.create_time_datetime desc")
            List<Map<String, Object>> RxOrderList(@Param("orderno") String orderno);

            

            @Select("select b.head as Head,b.`name` as Name,c.name_nvarchar as depName,b.title as Title"
            + " from order_main a"
            + " inner join doctor_base_info b on a.dc_id_bigint = b.dc_id"
            + " inner join h_hospital_department c on b.depart_id = c.id_bigint"
            + " where a.order_no_nvarchar = #{orderno}")
            Map<String, Object> docInfo(@Param("orderno") String orderno);

    class SqlBuilder {
        public String ListSQL(@Param("orderno") String orderno, @Param("atype") int atype,
                @Param("dname") String dname, @Param("cname") String cname, @Param("ocstatus") int ocstatus,
                @Param("skip") int skip, @Param("limit") int limit) {
            String sql = "with paget as (" + " select a.order_no_nvarchar from order_main a"
                    + " inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join doctor_base_info c on a.dc_id_bigint = c.dc_id" + " where 1=1";
            if (!orderno.isEmpty() && orderno.length() > 0)
                sql += " and a.order_no_nvarchar like CONCAT('%',#{orderno},'%')";
            if (atype != 0)
                sql += " and a.a_type_int = #{atype}";
            if (!dname.isEmpty() && dname.length() > 0)
                sql += " and c.name like CONCAT('%',#{dname},'%')";
            if (!cname.isEmpty() && cname.length() > 0)
                sql += " and b.name_nvarchar like CONCAT('%',#{cname},'%')";
            if (ocstatus != -1)
                sql += " and a.oc_status_tinyint = #{ocstatus}";
            sql += " order by a.create_time_datetime desc" + " limit #{skip},#{limit})"
                    + " select a.order_no_nvarchar as OrderNO,cast(a.a_type_int as SIGNED INTEGER) as AType,d.name_nvarchar as depName,c.name as dName,"
                    + " b.name_nvarchar as Name,a.create_time_datetime as CreateTime,a.ask_money_money as AskMoney,cast(a.oc_status_tinyint as SIGNED INTEGER) as OCStatus"
                    + " from paget p" + " inner join order_main a on a.order_no_nvarchar = p.order_no_nvarchar"
                    + " inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join doctor_base_info c on a.dc_id_bigint = c.dc_id"
                    + " inner join h_hospital_department d on c.depart_id = d.id_bigint"
                    +" order by a.create_time_datetime desc ";
            return sql;
        }

        public String ListCountSQL(@Param("orderno") String orderno, @Param("atype") int atype,
                @Param("dname") String dname, @Param("cname") String cname, @Param("ocstatus") int ocstatus) {
            String sql = "" + " select count(a.order_no_nvarchar) from order_main a"
                    + " inner join order_patients_info b on a.order_no_nvarchar = b.order_no_nvarchar"
                    + " inner join doctor_base_info c on a.dc_id_bigint = c.dc_id" + " where 1=1";
            if (!orderno.isEmpty() && orderno.length() > 0)
                sql += " and a.order_no_nvarchar like CONCAT('%',#{orderno},'%')";
            if (atype != 0)
                sql += " and a.a_type_int = #{atype}";
            if (!dname.isEmpty() && dname.length() > 0)
                sql += " and c.name like CONCAT('%',#{dname},'%')";
            if (!cname.isEmpty() && cname.length() > 0)
                sql += " and b.name_nvarchar like CONCAT('%',#{cname},'%')";
            if (ocstatus != -1)
                sql += " and a.oc_status_tinyint = #{ocstatus}";
            return sql;
        }
    }
}
